﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;

namespace ARS.Common.Helper
{
    /// <summary>
    /// 调用系统命令行
    /// </summary>
    public class ExecCommondHelper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="commond"></param>
        /// <param name="_param"></param>
        /// <param name="useShellExecute">区别，不能进行数据输出</param>
        /// <param name="outputConsonle">是否输出到控制台</param>
        public static void RunCommand(string commond = "dotnet", string _param = "--info")
        {
            try
            {
                Log.WriteLine($"------执行命令-----------\r\n {commond} {_param}\r\n", ConsoleColor.Yellow);
                ProcessStartInfo psi = null;
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    psi = new ProcessStartInfo(commond, _param) { RedirectStandardOutput = true };

                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    psi = new ProcessStartInfo
                    {
                        FileName = "/bin/bash",
                        UseShellExecute = false,
                        RedirectStandardOutput = true,
                        Arguments = string.Format($"-c \"{commond} {_param}")
                    };
                }
                //启动
                if (psi == null)
                    return;
                var proc = Process.Start(psi);
                if (proc == null)
                {
                    Console.WriteLine("Can not exec.");
                }
                else
                {

                    using (var sr = proc.StandardOutput)
                    {
                        while (!sr.EndOfStream)
                        {
                            Console.WriteLine(sr.ReadLine());
                        }
                    }
                    if (!proc.HasExited)
                    {
                        proc.Kill();
                    }
                }

            }
            catch (Exception ex)
            {
                Log.WriteLine($"Message:{ex.Message}\r\nStackTrace: {ex.StackTrace}", ConsoleColor.Red);
            }
        }

    }

    /// <summary>
    /// 日志输出
    /// </summary>
    public static class Log
    {
        private static object _locker = new object();
        public static void WriteLine(string centent, ConsoleColor colors = ConsoleColor.White, bool logtime = true)
        {
            lock (_locker)
            {
                Console.ForegroundColor = colors;
                if (logtime)
                    Console.WriteLine($"{centent}  {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
                else
                    Console.WriteLine(centent);
                Console.ResetColor();
            }
        }
    }
}
